<?php

namespace App\Exports;

use App\Models\GoodsType;
use App\Models\Orders;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\Exportable;
use Illuminate\Contracts\Support\Responsable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;

class dumpExcel implements Responsable, FromQuery, WithMapping, WithHeadings, WithTitle, ShouldAutoSize
{
    
    protected $model;
    protected $goodsType;

    public function __construct(Orders $order,GoodsType $goodsType)
    {
        $this->model = $order;
        $this->goodsType = $goodsType;
    }

	use Exportable;
	private $fileName = '订单列表';
	protected $days;
    
    protected $stime;
    protected $etime;
    public function withinDays($data, $days)
    {
        $this->days = $days;
        
        $this->stime = $data['stime'];
        $this->etime = $data['etime'];
        $this->fileName = $this->days.'.xlsx';
        return $this;
    }

    public function query()
    {
        ini_set('memory_limit','500M'); 
        set_time_limit(0);//设置超时限制为0分钟
        $merchant = auth('admin')->user(); // 商户ID
        $merchantId = $merchant->id;
        $where[] = ['merchant_orders.operation_id','=',$merchantId];

        if($merchant->type != 4 && $merchant->type != 1 && $merchant->type != 2){
            unset($where);
            $where[] = ['merchant_orders.merchant_id','=',$merchantId];
           
        }
        if($merchant->type == 1){$where = [];}
        if($this->stime){
            $where[] = ['orders.created_at', '>', $this->stime];
        }
        if($this->etime){
            $etime = date("Y-m-d H:i:s", strtotime("$this->etime +1 day"));
            $where[] = ['orders.created_at', '<', $etime];
        }
        $result = $this->model
            ->leftJoin('merchant_orders','merchant_orders.order_id','=','orders.id')
            ->leftJoin('goods_orders','goods_orders.order_id','=','orders.id')
            ->leftJoin('goods','goods.id','=','goods_orders.goods_id')
            ->select('orders.id','goods.goods_name as goodsname','goods.goods_image','orders.ordersn as order_sn','orders.machines_imel as machines_imel','orders.machines_name','orders.goods_total_price as goods_total_price','orders.order_total_price','orders.payment',
            'orders.order_status','orders.refund_status','orders.payment_time','orders.created_at','goods.category_id','orders.freightway_id'
            )
            //->filter(request()->all())
            ->where('merchant_orders.merchant_id',$merchantId)
            ->where($where)
            ->whereNotIn('orders.order_status',[0])
            ->orderBy('orders.created_at', 'asc');
            //->orderBy('id','desc');
        return $result;
    }
    public function map($results): array
    {
        return [
            $results->order_sn,
            $results->goodsname,
            $results->machines_imel,
            $results->machines_name,
            $results->order_total_price,
            $results->created_at,
            'payMent' => payment($results->payment),
            'orderStatust' => order_status($results->order_status = 2),
            $results->created_at,
            //'startTime' => created_at($results->created_at),
            'refundStatus' => refund($results->refund_status)
        ];
    }

    public function headings(): array
    {
        return [
            '订单号',
            '商品名称',
            '设备号IMEL',
            '设备名称',
            '商品单价',
            '下单时间',
            '支付方式',
            '订单状态',
            '支付时间',
            '退款状态',
        ];
    }

    public function title(): string
    {
        return '订单列表';
    }
}
